import { _decorator, Color, Component, math, MotionStreak, Node, Sprite, v2, Vec2, Vec3 } from 'cc';
import { PoolManager } from './PoolManager';
import { FallItemManager } from './FallItemManager';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('FallItem')
export class FallItem extends Component {
    @property(Sprite)
    public Icon: Sprite = null;

    @property(MotionStreak)
    MotionStreak: MotionStreak = null;

    private _lifetime: number;
    private _speed: number;
    private _timer: number = 0;
    private _offset: number = 10;
    private _velocity: Vec2 = Vec2.ZERO;
    private _gravity: number = 0;
    private _repulsionConstant: number = 0;
    private _subx: number = 1000;
    private _suby: number = 1000;
    private _dis: number = 300;

    private _addVelocity: Vec2 = new Vec2();
    private _isPause: boolean = false;

    init(parent: Node, worldPos: Vec3, lifetime: number, speed: number, gravity: number, repulsionConstant: number, subx: number, suby: number, dis: number) {
        this.node.setParent(parent);
        this.node.setWorldPosition(this.getOffsetPos(worldPos.clone()));
        this._lifetime = lifetime;
        this._speed = speed;
        this._timer = 0;
        this._gravity = gravity;
        this._repulsionConstant = repulsionConstant;
        this._subx = subx
        this._suby = suby
        this._dis = dis;
        this._addVelocity = v2(0, 0);
        //设置颜色
        var color: Color = this.getRandomColor();
        this.Icon.color = color
        this.MotionStreak.color = color;

        // 1. 随机生成 X 轴方向
        let randomX = Math.random() * 2 - 1; // -1 到 1 之间的随机数，表示向左或向右

        // 2. 设置方向向量，Y 为负，表示向下，X 为随机值
        let dir = v2(randomX, -5).normalize();  // Y 固定为 -1，X 方向随机

        // 3. 设置速度向量
        this._velocity = dir.multiplyScalar(speed);

    }

    protected update(dt: number): void {
        if (!FallItemManager.Instance.IsStart) return;
        if (this._isPause) return;
        this._timer += dt;
        this._velocity.y += dt * this._gravity;
        this.node.setWorldPosition(this.node.getWorldPosition().add3f((this._velocity.x + this._addVelocity.x) * dt, (this._velocity.y + this._addVelocity.y) * dt, 0));

        if (FallItemManager.Instance.checkOut(this.node.getWorldPosition())) {
            FallItemManager.Instance.removeItem(this.node);
        }

        const dis: number = FallItemManager.Instance.getDis(this.node);
        if (dis < this._dis) {
            let forceMagnitude = this._repulsionConstant / Math.pow(dis, 1.4);
            // let forceMagnitude = this._repulsionConstant / dis * 10;
            // 计算排斥力的方向
            let direction = FallItemManager.Instance.getDir(this.node);
            // 计算排斥力向量
            let force = direction.multiplyScalar(forceMagnitude);
            // FallItemManager.Instance.consoleMaxY(force.y);
            // 施加排斥力到当前物体的速度
            this._addVelocity = this._addVelocity.add2f(force.x, force.y);
            // this._addVelocity = this._addVelocity.add2f(force.x, math.clamp(force.y, 0, 10));
        } else {
            // this._addVelocity = v2(this._addVelocity.x / Math.abs(this._addVelocity.x) * (Math.abs(this._addVelocity.x) - dt * this._subx), this._addVelocity.y / Math.abs(this._addVelocity.y) * (Math.abs(this._addVelocity.y) - dt * this._suby));
        }
        this._addVelocity = v2(this._addVelocity.x / Math.abs(this._addVelocity.x) * (Math.abs(this._addVelocity.x) - dt * this._subx), this._addVelocity.y / Math.abs(this._addVelocity.y) * (Math.abs(this._addVelocity.y) - dt * this._suby));

    }

    // 生成一个随机的 cc.Color
    getRandomColor(): Color {
        let r = Math.floor(Math.random() * 256);
        let g = Math.floor(Math.random() * 256);
        let b = Math.floor(Math.random() * 256);
        return new Color(r, g, b, 255);
    }

    getOffsetPos(pos: Vec3) {
        return pos.add3f(math.randomRange(-this._offset, this._offset), 0, 0);
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }
}


